﻿using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;

namespace TSNPro.Utils
{
    public static class JsonUtils
    {
        // Load JSON Object from a file
        public static bool LoadJsonObject(string title, ref JObject jsonObj)
        {
            string deviceType = GetDeviceType();
            string dirS = $"./Config/{deviceType}";
            string fn = Path.Combine(dirS, $"{title}.json");

            JObject loadObj = LoadJsonFile(fn);
            jsonObj = loadObj;
            return true;
        }

        // Save JSON Object to a file
        public static bool SaveJsonObject(string title, JObject jsonObj)
        {
            string fn = Path.Combine("./Config", GetDeviceType(), $"{title}.json");
            return SaveJsonFile(fn, jsonObj);
        }

        // Load main configuration JSON object
        public static JObject LoadMainConfig()
        {
            JObject mainObj = LoadJsonFile("./Config/Main.json", false);
            return mainObj;
        }

        // Helper method to load a JSON file into a JObject
        public static JObject LoadJsonFile(string filePath, bool throwOnError = true)
        {
            try
            {
                if (File.Exists(filePath))
                {
                    string fileContent = File.ReadAllText(filePath);
                    return JObject.Parse(fileContent);
                }
                else
                {
                    if (throwOnError)
                    {
                        throw new FileNotFoundException($"File not found: {filePath}");
                    }
                    return null;
                }
            }
            catch (Exception ex)
            {
                if (throwOnError)
                {
                    Console.WriteLine($"Error loading JSON from file: {ex.Message}");
                    throw;
                }
                return null;
            }
        }

        // Helper method to save a JSON object to a file
        public static bool SaveJsonFile(string filePath, JObject jsonObj)
        {
            try
            {
                string dirPath = Path.GetDirectoryName(filePath);
                if (!Directory.Exists(dirPath))
                {
                    Directory.CreateDirectory(dirPath);
                }

                File.WriteAllText(filePath, jsonObj.ToString());
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error saving JSON to file: {ex.Message}");
                return false;
            }
        }

        // Placeholder method to simulate getting device type
        public static string GetDeviceType()
        {
            JObject mainObj = LoadMainConfig();
            string deviceType = mainObj["DeviceType"].ToString();
            return deviceType;
        }
    }
}
